import datetime
from typing import List, Dict
from pydantic import Field, BaseModel
from package.fastapi.schema import Page, BatchId


class VerifySampler(BaseModel):
    log: str = Field(..., title='验证样本') 


class EditParserGroup(BaseModel):
    name: str = Field(..., title='规则组名')
    description: str = Field('', title='规则组描述')


class CreateParserGroup(EditParserGroup):
    pass


class ParserGroupOut(CreateParserGroup):
    id: str = Field(..., title='id')


class ParserGroupOut(CreateParserGroup):
    id: str = Field(..., title='ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageParserGroups(Page):
    """获取页码条目"""
    data: List[ParserGroupOut] = Field(..., title='')


class _ExtractConfigure(BaseModel):
    pos: int = Field('', title='字段名')
    size: int = Field(None, title='长度')
    value: str = Field(None, title='截取文本值')


class _Extract(BaseModel):
    configure: Dict[str, _ExtractConfigure] = Field({}, title='划词配置')
    pattern: str = Field('', title='划词配置对应的正则表达式')


class _Pipeline(BaseModel):
    extract: _Extract = Field({}, title='字段提取')
    transforms: List = Field([], title='字段转换')


class ExtractAutoRegex(BaseModel):
    data: dict = Field(..., title='样本')
    extract: _Extract = Field({}, title='字段提取')


class Transform(BaseModel):
    data: dict = Field({}, title='校验数据')
    transforms: List = Field([], title='字段转换')


class GroupVerify(BaseModel):
    data: dict = Field({}, title='校验数据')


class EditParserRule(_Pipeline):
    sample: str = Field('', title='样本')
    identifiers: List[dict] = Field([], title='特征')    

    priority: int = Field(10, title='解析规则优先级')
    model_id: str = Field(None, title='数据模型ID')


class CreateParserRule(EditParserRule):
    name: str = Field(..., title='规则名')


class ParserRuleOut(CreateParserRule):
    id: str = Field(..., title='ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageParserRules(Page):
    """获取页码条目"""
    data: List[ParserRuleOut] = Field(..., title='')


class EditParserLib(_Pipeline):
    name: str = Field(..., title='名称')
    description: str = Field(..., title='描述')
    sample: str = Field(..., title='样本')


class CreateParserLib(EditParserLib):
    pass


class ParserLibOut(CreateParserLib):
    id: str = Field(..., title='ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageParserLibs(Page):
    """获取页码条目"""
    data: List[ParserLibOut] = Field(..., title='')



class _Mapping(BaseModel):
    key: str = Field(..., title='原始值')
    value: str = Field(..., title='映射值')


class EditParserMap(BaseModel):
    name: str = Field(..., title='名称')
    configure: dict = Field({}, title='映射表', example={'middle': '中危'})


class CreateParserMap(EditParserMap):
    pass


class ParserMapOut(CreateParserMap):
    id: str = Field(..., title='ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageParserMaps(Page):
    """获取页码条目"""
    data: List[ParserMapOut] = Field(..., title='')
